package com.taobao.home.service.impl;


import com.taobao.base.model.RestResponse;
import com.taobao.home.mapper.ProcessHistoryMapper;
import com.taobao.home.mapper.ProcessMapper;
import com.taobao.home.model.po.Process;
import com.taobao.home.model.po.ProcessHistory;
import com.taobao.home.service.ProcessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProcessServiceImpl implements ProcessService {

    @Autowired
    private ProcessMapper processMapper;

    @Autowired
    private ProcessHistoryMapper processHistoryMapper;

    @Override
    public List<Process> getProcessList(int shardIndex, int shardTotal, int count) {

        List<Process> processList = processMapper.selectListByShardIndex(shardTotal, shardIndex, count);

        return processList;
    }

    @Override
    public boolean startTask(long taskId) {
        int result = processMapper.startTask(taskId);
        return result <= 0 ? false : true;
    }

    @Override
    public void saveProcessFinishStatus(long taskId, String status, String errorMsg) {

        //要更新的任务
        Process process = processMapper.selectById(taskId);
        if (process == null) {
            return;
        }
        //如果任务执行失败
        if (status.equals("3")) {
            //更新MediaProcess表的状态
            process.setStatus("3");
            process.setFailCount(process.getFailCount() + 1);//失败次数加1
            process.setErrormsg(errorMsg);
            processMapper.updateById(process);
            //更高效的更新方式
//            mediaProcessMapper.update()
            //todo:将上边的更新方式更改为效的更新方式
            return;

        }


        //======如果任务执行成功======
//        //文件表记录
//        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
//        //更新media_file表中的url
//        mediaFiles.setUrl(url);
//        mediaFilesMapper.updateById(mediaFiles);

        //更新Process表的状态
        process.setStatus("2");
        process.setUpdateTime(LocalDateTime.now());
        processMapper.updateById(process);


        //将Process表记录插入到ProcessHistory表
        ProcessHistory processHistory = new ProcessHistory();
        BeanUtils.copyProperties(process, processHistory);
        processHistoryMapper.insert(processHistory);

        //从Process删除当前任务
        processMapper.deleteById(taskId);


    }

    @Override
    public RestResponse<List<Process>> test1() {

        return RestResponse.success(processMapper.selectList(null));

    }

    @Override
    public RestResponse<String> test2() {

        Process process = new Process();

        for (int i = 0; i < 70; i++) {
            process.setFileId(i+7);
            process.setTypes(1);
            process.setStatus("1");
            process.setFailCount(0);
            process.setErrormsg("errormsg" + i);
            process.setUpdateTime(LocalDateTime.now());
            int insert = processMapper.insert(process);
            if (insert != 1) {
                return RestResponse.validfail("fail");
            }
        }

        return RestResponse.success("no");

    }
}
